package com.imti.contentproviderdemo;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;

public class MyDBHelper extends SQLiteOpenHelper
{

    public static final String TABLE_NAME = "student_info";

    public static final String STU_NAME = "stu_name";

    public static final String STU_CLASS = "stu_class";

    public static final String STU_COMPANY = "stu_company";

    public MyDBHelper(@Nullable Context context,
        @Nullable String name,
        @Nullable CursorFactory factory, int version)
    {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(
            "create table " + TABLE_NAME + "(_id integer primary key autoincrement,"
                + " " + STU_NAME + " text not null, "
                + STU_CLASS + " text, "
                + STU_COMPANY + " text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {

    }

    /**
     * 插入到学生就业信息表
     *
     * @param cv 学生信息
     * @return 新插入数据的_id
     */
    public long insertStuInfo(ContentValues cv)
    {
        return getWritableDatabase().insert(TABLE_NAME,
            null, cv);
    }

    public int deleteStuByName(String name)
    {
        return getWritableDatabase()
                   .delete(TABLE_NAME,
                       STU_NAME + "=?", new String[]{name});
    }

    public int deleteStu(String where, String[] whereArgs)
    {
        return getWritableDatabase().delete(TABLE_NAME, where, whereArgs);
    }
}
